# -*- coding:utf-8 -*-
import pymysql

class DBService:
  params = { 'host':'127.0.0.1','port':3306,'user':'root','passwd':'R123456#','db':'tcikma_data'}

  def select(self, key, key_value, table_name):
    db = pymysql.connect(
      host = self.params['host'],
      port = self.params['port'],
      user = self.params['user'],
      passwd = self.params['passwd'],
      db = self.params['db']) # 数据库名称
    # 使用cursor()方法创建一个游标对象cur （可以理解为激活数据库）
    cur = db.cursor()

    # 使用execute()执行SQL语句
    sqlStr = "select * from `"+table_name+"` where "+key+'='
    if type(key_value) == str: sqlStr += ('\"'+key_value+'\"') 
    else: sqlStr += key_value
    cur.execute(sqlStr)

    # 使用fetchall()方法获取查询结果 (接收全部的返回结果)
    data = cur.fetchall()
    # 解决游标遍历慢的方法：一行一行去遍历，而不是一下全部读取出来
    # for i in cur:
    #     print (i)
    db.close()
    print(data)
    return data